<?php
/*
 * MenusController.php
 * Handle the CRUD for menus.
 */

require_once 'MenusModel.php';
require_once 'MenusView.php';

class MenusController
{
    public function DoControll()
    {
        $menusView = new MenusView();
        $menusModel = new MenusModel();


        if ( $menusView->GetAddMenuOption() === "Add" )
        {
            $menusModel->AddMenu( $menusView->GetAddMenuName() );
        }
        
        if ( $menusView->GetListMenuOption() === "Delete" )
        {
            $menusModel->RemoveMenu( $menusView->GetListMenuID() );
        }

        if ( $menusView->GetEditMenuOption() === "Save" )
        {
            $menusModel->SaveMenu( $menusView->GetEditMenuName(), $menusView->GetEditMenuID() );
        }
        
        if ( $menusView->GetAddMenuItemOption() === "Add Menu Item" )
        {
            $menusModel->AddPath( $menusView->GetAddMenuItemText(), $menusView->GetAddMenuItemID(), $menusView->GetAddMenuItemPageID() );
        }
        
        switch ( $menusView->GetListMenuItemOption() )
        {
            case "Delete" :
                $menusModel->DeleteMenuItem( $menusView->GetListMenuItemID() );
                break;
            case "Up" :
                $menusModel->MoveUpMenuItem( $menusView->GetListMenuItemID() );
                break;
            case "Down" :
                $menusModel->MoveDownMenuItem( $menusView->GetListMenuItemID() );
                break;
            case "In" :
                $menusModel->MoveInMenuItem( $menusView->GetListMenuItemID() );
                break;
            case "Out" :
                $menusModel->MoveOutMenuItem( $menusView->GetListMenuItemID() );
                break;
        }
        
        if ( $menusView->GetEditMenuItemOption() === "Save" )
        {
            $menusModel->SaveMenuItem( $menusView->GetEditMenuItemID(), $menusView->GetEditMenuItemName(), $menusView->GetEditMenuItemText(), $menusView->GetEditMenuItemPageID() );
        }
        
        
        $html = '';

        if ( $menusView->GetListMenuOption() === "Edit" )
        {
            $html .= $menusView->DrawEditMenuForm( $menusModel->GetMenu( $menusView->GetListMenuID() ) );
            $html .= $menusView->DrawAddPathForm( $menusView->GetListMenuID(), $menusModel->GetPages() );
            
            if ( $menusView->GetListMenuItemOption() === "Edit" )
            {
                $html .= $menusView->DrawEditMenuItemForm( $menusModel->GetPath( $menusView->GetListMenuItemID() ), $menusModel->GetPages() );
            }
            
            $html .= $menusView->DrawMenuItemsList( $menusModel->GetPaths( $menusView->GetListMenuID() ) );
            
        } else
        {
            $html .= $menusView->DrawNewMenuForm();
        }

        $html .= $menusView->DrawMenusList( $menusModel->GetAllMenus() );

        return $html;
    }
}
